package com.example.springboot01.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springboot01.entity.Student;
import com.example.springboot01.req.PageQueryReq;
import com.example.springboot01.resp.Response;
import com.example.springboot01.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/v1/student")
public class StudentController {

    @Resource
    private StudentService service;

    @PostMapping
    public Response save(@RequestBody Student student){
        service.addStudent(student);
        return Response.ok(null).setMsg("新增成功");
    }

    @PutMapping
    public Response update(@RequestBody Student student){
        service.updateById(student);
        return Response.ok(null).setMsg("更新成功");
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable Integer id){
        service.removeById(id);
        return Response.ok(null).setMsg("删除成功");
    }

    @GetMapping
    public Response list(String search){
        return Response.ok(
                service.listWithDetail(search)
        );
    }


    @GetMapping("/page")
    public Response page(PageQueryReq req, HttpSession session){
        return Response.ok(this.service.getPage(req, session));
    }
}
